<!--  Ofri Keidar 302933981 group 05 -->

<%@page import="java.util.Enumeration"%>
<%@page import="model.database.DBException"%>
<%@page import="view.WebCommons"%>
<%@page import="controller.CmnTransactionsCtrl"%>
<%@page import="builder.ControllerBuilder"%>
<%@page import="controller.CommonValues"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Confirm details...</title>
</head>
<body>

	<%
		// get username and password
			String userName = request.getParameter(WebCommons.USER_NAME);
			String password = request.getParameter(WebCommons.PASSWORD);

			// clear session's attributes, in case logging in as a new user
			@SuppressWarnings("rawtypes")
			Enumeration attNames = session.getAttributeNames();
			while (attNames.hasMoreElements()) {

				// get current attribute name
				String currAttribute = (String) attNames.nextElement();
		
				// remove attribute
				session.removeAttribute(currAttribute);
			}

			// attach system builder to session
			CommonValues.permissions permission = null; // variable used to hold user's permission
			try {
		
				ControllerBuilder sysBuilder = new ControllerBuilder();
				session.setAttribute(WebCommons.BUILDER, sysBuilder);
				
				// attach common-controller to session
				CmnTransactionsCtrl cmnController = sysBuilder.getCommonController();
				session.setAttribute(WebCommons.COMMON_CONTROLLER, cmnController);
				
				// attach user-name and password to session
				session.setAttribute(WebCommons.USER_NAME, userName);
				session.setAttribute(WebCommons.PASSWORD, password);
				
				// varify details
				permission = sysBuilder.getUserPermission(userName, password);

			} catch (DBException e) {
		
				// move to error page
				response.sendRedirect("commonPages/ErrorPage.jsp");
			}

			// check if error in user or password
			if (permission == null) {

		// wrong details, ask again and display error mesage
	%>
			<%@ include file="Login.jsp"%>
			<span class="notify">Username or password are incorrect, please try again...</span>
			<%
			
		} else if (permission.equals(CommonValues.permissions.ADMINISTRATOR)) {

			// move to administrator menu
			response.sendRedirect("menus/AdminMenu.jsp");

		} else if (permission.equals(CommonValues.permissions.CUSTOMER)) {

			// move to customer menu
			response.sendRedirect("menus/CustomerMenu.jsp");

		} else if (permission.equals(CommonValues.permissions.EMPLOYEE)) {

			// move to employee menu
			response.sendRedirect("menus/EmployeeMenu.jsp");

		} else if (permission.equals(CommonValues.permissions.MANAGER)) {

			// move to manager menu
			response.sendRedirect("menus/ManagerMenu.jsp");
		}
	%>
</body>
</html>